package com.soxing.braindata.service.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.soxing.braindata.dao.domain.request.ExperimentSearchRequest;
import com.soxing.braindata.dao.domain.request.SearchRequest;
import com.soxing.braindata.dao.domain.request.TesteeSearchRequest;
import com.soxing.braindata.dao.entity.Experiment;
import com.soxing.braindata.dao.entity.Research;
import com.soxing.braindata.dao.entity.Testee;
import com.soxing.braindata.dao.service.IResearchService;
import com.soxing.braindata.dao.service.ITesteeService;
import com.soxing.braindata.domain.vo.TesteeVo;
import com.soxing.common.utils.response.MyPage;
import com.soxing.common.utils.response.WebResponse;
import com.soxing.common.utils.util.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author :durango
 * @date :2022/7/3 11:22
 * @description 被试管理实现
 */
@Slf4j
@Service
public class TesteeManagerImpl extends AbstractManager<Testee> implements ITesteeManager {
    @Resource
    private ITesteeService testeeService;
    @Resource
    private IResearchService researchService;
    @Resource
    private IResearchManager researchManager;
    @Resource
    private IExperimentManager experimentManager;

    @Override
    public IService<Testee> getService() {
        return testeeService;
    }

    @Override
    public LambdaQueryWrapper<Testee> wrapSearchRequest(SearchRequest request) {
        TesteeSearchRequest searchRequest = (TesteeSearchRequest) request;
        if (request == null) {
            searchRequest = new TesteeSearchRequest();
        }
        Testee obj = ObjectMapper.map(searchRequest, Testee.class);
        LambdaQueryWrapper<Testee> queryWrapper = new LambdaQueryWrapper<>(obj);
        if (StringUtils.isNotBlank(searchRequest.getName())) {
            obj.setName(null);
            queryWrapper.like(Testee::getName, searchRequest.getName());
        }
        return queryWrapper;
    }

    @Override
    public WebResponse<TesteeVo> getVoByNo(String testeeNo) {
        List<Testee> list = testeeService.list(new LambdaQueryWrapper<Testee>().eq(Testee::getTesteeNo, testeeNo));
        if (CollectionUtils.isEmpty(list)) {
            log.error("testeeNo:{}不存在", testeeNo);
            return WebResponse.fail("被试不存在");
        }
        Testee testee = list.get(0);
        WebResponse<Research> response = researchManager.getBysubjectPkId(testee.getSubjectPkId());
        Research research = response.getData();
        if (research == null) {
            log.error("testeeNo:{},subjectPkId:{}对应课题不存在", testeeNo, research.getSubjectPkId());
            return WebResponse.fail("被试对应的课题不存在");
        }

        ExperimentSearchRequest experimentSearchRequest = new ExperimentSearchRequest();
        experimentSearchRequest.setSubjectPkId(research.getSubjectPkId());
        experimentSearchRequest.setPageSize(100);
        WebResponse<MyPage<Experiment>> experimentResponse = experimentManager.search(experimentSearchRequest);
        MyPage<Experiment> experimentResponseData = experimentResponse.getData();
        List<Experiment> experimentList = experimentResponseData.getRecords();

        TesteeVo testeeVo = TesteeVo.builder().testee(testee).research(research).experimentList(experimentList).build();
        return WebResponse.success(testeeVo);
    }
}
